package com.cai.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.cai.dao.CheckGroupDao;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.pojo.CheckGroup;
import com.cai.service.CheckGroupService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务实现类
 */

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组,关联检查项
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //1.t_checkgroup表 2.t_checkgroup_checkitem表
        checkGroupDao.add(checkGroup);
        //操作关联表t_checkgroup_checkitem
        Integer checkGroupId = checkGroup.getId();
       setCheckGroupAndCheckItem(checkGroupId, checkitemIds);


    }

    //检查项分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //使用分页助手
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> checkGroups = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        long total = checkGroups.getTotal();
        List<CheckGroup> rows = checkGroups.getResult();
        return new PageResult(total, rows);
    }

    //删除检查项,
    public void deleteById(Integer id) {
        //删除前需要查询是否已经关联了中间表,如果关联了就无法删除
        long count = checkGroupDao.findCheckGroupAndCheckItem(id);
        if (count > 0) {
            new RuntimeException();
        }
        checkGroupDao.deleteById(id);
    }

    //根据id查询检查组
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据检查组id查询检查项
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //编辑检查项,同时需要更新和检查项的关联关系
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

        //1.先根据检查组id清除中间表的关联关系
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //2.重新建立中间关联关系
        Integer checkGroupId = checkGroup.getId();
        setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
        //3.更新检查组
        checkGroupDao.edit(checkGroup);
    }

    //查询所有检查组
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    //定义方法用于建立中间表关联
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

}
